package com.ruoyi.operation.order.management.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.management.domain.Management;
import com.ruoyi.operation.order.management.service.IManagementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author wangzhiwei phone(WeChat)):15062775766
 * @date 2020/5/3 17:09
 */
@Controller
@RequestMapping(value = "/management/management")
public class ManagementController extends BaseController {

    // 定义一个跳转到页面的路径
    private String prefix = "order/management/";

    @Autowired
    IManagementService iManagementService;

    /**
     * 跳转到数据页面
     * @return
     */
    @RequiresPermissions(value = "management:management:management")
    @GetMapping()
    String management(){
        return prefix + "management";
    }

    /**
     * 分页查询所有的营销数据
     * @param management
     * @return
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "management:management:management")
    @ResponseBody
    public TableDataInfo findAllList(Management management){
        startPage();
        List<Management> managementList = iManagementService.findAll(management);
        return getDataTable(managementList);

    }

    /**
     * 改变联系的状态
     * @param management
     * @return
     */
    @RequestMapping(value = "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Management management){
        return toAjax(iManagementService.changeStatus(management));
    }

    /**
     * 改变打开弹出框的状态
     * @param dataId
     * @return
     */
    @RequestMapping(value = "/changeIsOPen")
    @ResponseBody
    public AjaxResult changeIsOPen(String dataId){
        return toAjax(iManagementService.changeIsOPen(dataId));
    }

    /**
     * 跳转到淘客户的页面
     * @param dataId
     * @return
     */
    @RequestMapping(value = "/toAmoyHtml")
    String toAmoyHtml(String dataId, Model model,Management management){
        if (null != dataId || !"".equals(dataId)){
             management = iManagementService.findById(dataId);
        }
        model.addAttribute("management",management);
        return prefix + "amoyHtml";
    }

    /**
     * 改变状态未不在沟通
     * @param dataId
     * @return
     */
    @RequestMapping(value = "/noContact")
    @ResponseBody
    public AjaxResult noContact(String dataId){
        Management management = new Management();
        management.setRemark("不再联系");
        management.setDataId(dataId);
        management.setLinkStatus(3);
        management.setContactsNumber("1");
        return toAjax(iManagementService.noContact(management));

    }
    /**
     * 得到客户
     * @param dataId
     * @return
     */
    @RequestMapping(value = "/getCustomer")
    @ResponseBody
    public AjaxResult getCustomer(String dataId){
        Management management = new Management();
        management.setRemark("淘到客户");
        management.setDataId(dataId);
        management.setLinkStatus(1);
        management.setContactsNumber("1");
        return toAjax(iManagementService.noContact(management));
    }

    /**
     * 不在联系
     * @param dataId
     * @return
     */
    @RequestMapping(value = "/nextContact")
    @ResponseBody
    public AjaxResult nextContact(String dataId) throws Exception{
        // 定义变量，联系的次数
        Integer contactNumber = 0;
        // 点击的时候根据id查询对应的联系的次数
        if (null != dataId || !"".equals(dataId)){
            contactNumber = iManagementService.contacNumber(dataId);
            contactNumber += 1;
        }
        Management management1 = iManagementService.findById(dataId);
        Management management = new Management();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将String类型的时间数据转成Date类型
        Date date = sdf.parse(management1.getEnterDate());
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, 3);
        Date time = c.getTime();
        String preDay = sdf.format(time);
        switch (contactNumber){
            case 1:
                management.setRemark("下次联系1");
                management.setLinkStatus(2);
                management.setDataId(dataId);
                management.setIsOpen(0);
                management.setEnterDate(preDay);
                management.setContactsNumber(String.valueOf(contactNumber));
                break;
            case 2:
                management.setRemark("下次联系2");
                management.setLinkStatus(2);
                management.setDataId(dataId);
                management.setIsOpen(0);
                management.setEnterDate(preDay);
                management.setContactsNumber(String.valueOf(contactNumber));
                break;
            case 3:
                management.setRemark("不再联系");
                management.setDataId(dataId);
                management.setLinkStatus(3);
                management.setContactsNumber("3");
                management.setIsOpen(1);
                management.setEnterDate(preDay);
                break;
        }
        return toAjax(iManagementService.noContact(management));
    }

    /**
     * 导出Excel表格
     * @param management
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(Management management, HttpServletRequest request, HttpServletResponse response) throws Exception{
        iManagementService.exportExcel(management,request,response);
    }

}
